package com.effective.java.equals;

public class BrokenSymmetry01 {

	public static void main(String[] args) {

		String s1 = "Hello World!";
		CaseInsensitiveString s2 = new CaseInsensitiveString("HeLlo wOrLd!");
		System.out.println("s1.equals(s2) = " + s1.equals(s2));
		System.out.println("s2.equals(s1) = " + s2.equals(s1));
	}

	private static class CaseInsensitiveString {

		private final String string;

		public CaseInsensitiveString(String string) {

			if (string == null) {
				throw new IllegalArgumentException("Mandatory input parameter 'string' is null!");
			}
			this.string = string;
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public boolean equals(Object object) {

			if (object instanceof CaseInsensitiveString) {
				return this.string.equalsIgnoreCase(((CaseInsensitiveString) object).getString());
			}
			/*
			 * This breaks the symmetry requirement because now
			 * CaseInsensitiveString.equals(String) will probably result
			 * differently from String.equals(CaseInsensitiveString)
			 */
			else if (object instanceof String) {
				return this.string.equalsIgnoreCase((String) object);
			}
			else {
				return false;
			}
		}

		public String getString() {

			return this.string;
		}
	}
}
